﻿using HiStore.IServices.System;
using HiStore.Model.Entity.System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HiStore.WebApi.Controllers.System
{
    [Route("api/[controller]")]
    [ApiController]
    //使用HTTP动词表示增删改查资源， GET：查询，POST：新增，PUT：更新，DELETE：删除
    //返回结果必须使用JSON
    //HTTP状态码，在REST中都有特定的意义：200，201,202,204,400,401,403,500。比如401表示用户身份认证失败，403表示你验证身份通过了，但这个资源你不能操作。
    public class SysUserController : ControllerBase
    {
        public readonly ISysUserServices _sysUserServices;
        public SysUserController(ISysUserServices sysUserServices)
        {
            _sysUserServices = sysUserServices;
        }

        [HttpGet("{id}")]
        public IActionResult GetUser(long id)
        {
           var user=_sysUserServices.QueryById(id);
          return Ok(user);
        }

        [HttpPost]
        public IActionResult PostSysUser([FromBody] SysUser user)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            _sysUserServices.Insert(user);
            return Ok(user);
        }

        // DELETE: api/SysUsers/5
        [HttpDelete("{id}")]
        public IActionResult DeleteSysUser([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var sysUser = _sysUserServices.QueryById(id);
            if (sysUser == null)
            {
                return NotFound();
            }
            _sysUserServices.Delete(sysUser);
            return Ok(sysUser);
        }

        // PUT: api/SysUsers/5
        [HttpPut("{id}")]
        public IActionResult PutSysUser([FromRoute] long id, [FromBody] SysUser sysUser)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            if (id != sysUser.id)
            {
                return BadRequest();
            }
            var isBool=_sysUserServices.Update(sysUser);

            if (isBool)
            {
                return Ok();
            }
            return NoContent();
        }

    }
}
